﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon.Prompt;
using Baosight.ZNH.ZP.UI.Utils;

namespace Baosight.ZNH.ZP.UI.PopWindows
{
    /// <summary>
    /// 增添资源组
    /// </summary>
    /// Title:ResGInsert
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 梁琦
    /// Version: 1.0
    /// History:
    ///       2012-10-18  梁琦   [创建]
    ///  
    public partial class ResGInsert : EFViewCmes
    {
        private TypeBlock typeBlock = new TypeBlock();

        public ResGInsert(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(typeBlock.GetType());
            base.Initialize(initData);
        }

        /// <summary>
        /// 事前操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.ResGPanel.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    this.Commit2.Visibility = Visibility.Collapsed;
                    this.addRes.Visibility = Visibility.Collapsed;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    this.addRes.Visibility = Visibility.Collapsed;
                    this.Next1.Visibility = Visibility.Collapsed;
                    this.Commit2.Visibility = Visibility.Visible;
                    this.ResGName.IsReadOnly = true;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.InsertGrid.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 增添时确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            if (!this.ResGPanel.ValidateField())
            {
                this.ValidateMessagePopup();//提示输入不合法
                TempButton.IsEnabled = true;
                return;
            }

            EIInfo info = this.BuildSubmitEIInfo(eServiceName, Mode.ToString().ToLower(), "result");
            info.Set("roleId", this.EIDataContext.GetAttribute("roleId"));//在ZP15中用到
            EIBlock block = info.AddBlock("ResResult");
            block.SetColumns(this.resGrid.EIBlock.GetColumns());
            block.SetRows(this.resGrid.CheckedEIRows);
            this.Submit(info, DoCommitCompleted);
        }

        /// <summary>
        /// 修改时确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Commit2Click(object sender, RoutedEventArgs e) 
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            if (!this.ResGPanel.ValidateField())
            {
                this.ValidateMessagePopup();//提示输入不合法
                TempButton.IsEnabled = true;
                return;
            }
            EIInfo info = this.BuildSubmitEIInfo(eServiceName, Mode.ToString().ToLower(), "result");
            this.Submit(info, DoCommitCompleted);
        }

        /// <summary>
        /// 增添时查询资源按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Query_Click(object sender, RoutedEventArgs e)
        {
            this.OnQueryButtonClick(sender, e);
        }
        /// <summary>
        /// 上一步按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BeforeClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            this.addRes.Visibility = Visibility.Collapsed;
            this.ResGPanel.Visibility = Visibility.Visible;
            TempButton.IsEnabled = true;
        }

        /// <summary>
        /// 下一步按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Next1Click(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;
         
            if (!this.ResGPanel.ValidateField())
                {
                    this.ValidateMessagePopup();//提示输入不合法
                    TempButton.IsEnabled = true;
                    return;
                }
                //添加角色时，点击下一步
                this.addRes.Visibility = Visibility.Visible;
                this.ResGPanel.Visibility = Visibility.Collapsed;
               

                //判断是否已经查询过资源组
                if (this.resGrid.HasItems)
                {
                    TempButton.IsEnabled = true;
                    return;
                }
                EIInfo info = new EIInfo();
               // info.AddBlock(typeBlock.GetType());
                info.Set(EIConstants.ServiceNameKey, "ZP22");
                info.Set(EIConstants.MethodNameKey, "queryRes");
                this.Submit(info, QueryResGCompleted);
                return;
  
        }

        /// <summary>
        /// 查询资源组的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryResGCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                e.EIInfo.AddBlock(typeBlock.GetType());
                this.resGrid.UpdateData(e.EIInfo);
            }
            else
            {
                this.ValidateMessagePopup("出错了：" + e.EIInfo.Msg);
            }
            TempButton.IsEnabled = true;
        }

        /// <summary>
        /// 返回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        
        /// <summary>
        /// 消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }
    }
}
